''' <summary>
''' Developer Name : Mr.Teeradech Janwimalueng (http://teedech.blogspot.com, Thai Language Blog)
''' Date Created : 3 October 2007
''' Last Updated : -
''' License : Microsoft Permissive License (Ms-PL) v1.1
''' Version 1.0 beta
''' </summary>
''' <remarks>
''' </remarks>

Friend Class RailFenceCipherEngine

    Friend Function Encrypt(ByVal PlainText As String, ByVal Key As Byte) As String
        'find column number
        Dim ColumnNum As Byte = PlainText.Length \ Key
        If (PlainText.Length Mod Key) > 0 Then ColumnNum += 1

        Dim CharMatrix(Key - 1, ColumnNum - 1) As Char
        Dim CharArray(CharMatrix.Length - 1) As Char

        'order plain text string in metric (Array 2 dimensions)
        'top-down charecter order for each columns
        Dim Count As Byte = 0
        For CountCol As Byte = 0 To ColumnNum - 1
            For CountRow As Byte = 0 To Key - 1
                If Count <= PlainText.Length - 1 Then
                    CharMatrix(CountRow, CountCol) = PlainText(Count)
                    Count += 1
                Else
                    CharMatrix(CountRow, CountCol) = Nothing
                End If
            Next CountRow
        Next CountCol

        'Left-to-Right charecter order for each row to string
        Count = 0
        For CountRow As Byte = 0 To Key - 1
            For CountCol As Byte = 0 To ColumnNum - 1
                If Not CharMatrix(CountRow, CountCol) = Nothing Then
                    CharArray(Count) = CharMatrix(CountRow, CountCol)
                    Count += 1
                End If
            Next CountCol
        Next CountRow

        'return array of Char as String
        Return CharArray
    End Function

    Friend Function Decrypt(ByVal CipherText As String, ByVal Key As Byte) As String
        'find column number
        Dim ColumnNum As Byte = CipherText.Length \ Key
        If (CipherText.Length Mod Key) > 0 Then ColumnNum += 1

        'find emtry cell
        Dim EmptyNum As Byte
        Dim CharTotal As Byte = CipherText.Length
        EmptyNum = IIf(CharTotal Mod Key = 0, 0, Key - (CharTotal Mod Key))

        Dim CharMatrix(Key - 1, ColumnNum - 1) As Char
        Dim CharArray(CharMatrix.Length - 1) As Char

        'order cipher text string in metric (Array 2 dimensions)
        'Left-to-Right charecter order for each row
        Dim Count As Byte = 0
        For CountRow As Byte = 0 To Key - 1
            If CountRow <= (Key - 1 - EmptyNum) Then
                'case - non-emtry cell in row
                For CountCol As Byte = 0 To ColumnNum - 1
                    CharMatrix(CountRow, CountCol) = CipherText(Count)
                    Count += 1
                Next CountCol
            Else
                'case - emtry cell in row
                For CountCol As Byte = 0 To ColumnNum - 2
                    CharMatrix(CountRow, CountCol) = CipherText(Count)
                    Count += 1
                Next CountCol
            End If
        Next CountRow

        'Top-down charecter order for each columns to string
        Count = 0
        For CountCol As Byte = 0 To ColumnNum - 1
            For CountRow As Byte = 0 To Key - 1
                If Not CharMatrix(CountRow, CountCol) = Nothing Then
                    CharArray(Count) = CharMatrix(CountRow, CountCol)
                    Count += 1
                End If
            Next CountRow
        Next CountCol

        'return array of Char as String
        Return CharArray
    End Function

End Class
